﻿#include "filetreeservice.h"
#include <model/filemodel.h>
// #include <model/filetree.h>
#include <util/fileutil.h>
#include<qstack.h>



FileTreeService::FileTreeService() {

}



QList<FileModel*> FileTreeService::getChildren(QString fullPath) {
    QList<FileModel*> children;
    QList<FileModel*> files = FileUtil::listDir(fullPath);
    for (int i = 0; i < files.size(); i++) {
        children.append(new FileModel(files[i]->name(), files[i]->fullPath(), files[i]->type(), 0));
    }
    return children;
}



FileModel* FileTreeService::getAll(QString fullPath) {
    FileModel* root = new FileModel("root", "dir", fullPath, 0); // 此文件路径
    getAllRecursion(fullPath, root, -1);
    return root;
}



void FileTreeService::getAllRecursion(QString fullPath, FileModel* root, int deep) {
    QList<FileModel*> files = FileUtil::listDir(fullPath);
    if (files.size() == 0) {
        return;
    }
    FileModel* file = nullptr;
    deep++;
    for (int i = 0; i < files.size(); i++) {
        file = files[i];
        file->setTab(deep); // 标记深度
        if (file->type() == "dir") {
            root->childs.append(file);
            // qDebug()<<tree->fullPath;
            getAllRecursion(file->fullPath(), file, deep);
        } else {
            root->childs.append(file);
            // qDebug()<<tree->fullPath;
        }
    }
    file = nullptr;
    files.clear();
}



// 获取 model list
QList<FileModel*> FileTreeService::getList(FileModel * root) { // 深度遍历
    QList<FileModel*> res;
    QStack<FileModel*> stack;
    FileModel* tmp = root;

    //dfs
    while (tmp != nullptr) {
        // 获取可见的child
        QList<FileModel*> visibleChild = ListUtil::filter<FileModel*>(tmp->childs, [](FileModel* x)-> bool { return x->visible; });
        if (visibleChild.size() != 0) {
            for (int i = visibleChild.size() - 1; i > 0; i--) {
                FileModel* node = visibleChild[i];
                stack.push_back(node);
            }
            tmp = visibleChild[0]; // 获取第一个元素
        } else {
            if (stack.size() > 0) {
                tmp = stack.pop();
                res.append(tmp);
                // qDebug() << tmp->fullPath;
            } else {
                tmp = nullptr;
            }
        }
    }

    tmp = nullptr;
    return res;
}



FileModel* FileTreeService::searchTree(FileModel* root, QString key) {// 搜索
    FileModel* tmp = root;
    QQueue<FileModel*> queue;
    queue.enqueue(root);
    FileModel* res = nullptr;
    while (tmp != nullptr) {
        tmp = queue.dequeue();
        if (tmp->fullPath() == key) {
            res = tmp;
            // qDebug() << "找到节点";
            break;
        }
        foreach(FileModel* t , tmp->childs) {
            queue.enqueue(t);
        }
    }
    return res;
}



QList<FileModel*> FileTreeService::openNode(FileModel* root, QString fullPath) {// 点击节点展开
    // qDebug() << "打开" + fullPath;
    FileModel* node = searchTree(root, fullPath);
    foreach(FileModel* t , node->childs) {
        t->visible = !t->visible;
    }
    QList<FileModel*> mos = getList(root);
    return mos;
}
